package com.xayy.health.core.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.enums.CellExtraTypeEnum;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.CellExtra;
import com.xayy.health.mgt.model.BatchUploadData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangxa
 */
@Slf4j
public class CustomAnalysisEventListener extends AnalysisEventListener<BatchUploadData> {
    private int headRowNum;

    private String satelliteCode;


    private MultipartFile file;

    private Long fileLogo;


    private List<BatchUploadData> list = new ArrayList<>();

    private List<CellExtra> cellExtraList = new ArrayList<>();

    public CustomAnalysisEventListener(int headRowNum, MultipartFile file, Long fileLogo, String satelliteCode) {
        this.headRowNum = headRowNum;
        this.file = file;
        this.fileLogo = fileLogo;
        this.satelliteCode = satelliteCode;
    }

    @Override
    public void invoke(BatchUploadData excelData, AnalysisContext analysisContext) {
        log.info(" data -> {}", excelData);
        excelData.setFileName(file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf(".")));
        excelData.setFileLogo(fileLogo);
        excelData.setSatelliteCode(satelliteCode);
        list.add(excelData);
    }

    @Override
    public void extra(CellExtra extra, AnalysisContext context) {
        CellExtraTypeEnum type = extra.getType();
        switch (type) {
            case MERGE: {
                if (extra.getRowIndex() >= headRowNum) {
                    cellExtraList.add(extra);
                }
                break;
            }
            default: {
            }
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    public List<BatchUploadData> getList() {
        return list;
    }

    public List<CellExtra> getCellExtraList() {
        return cellExtraList;
    }
}